package com.hcrazy.manchester.app.services;

import java.util.List;

import org.joda.time.DateTime;

import com.hcrazy.manchester.app.jdbc.NewsInfoDAO;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.NewsInfo;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.utils.Constant;

public class NewsInfoService {

	private NewsInfoDAO newsInfoDAO;

	public NewsInfoDAO getNewsInfoDAO() {
		return newsInfoDAO;
	}

	public void setNewsInfoDAO(NewsInfoDAO newsInfoDAO) {
		this.newsInfoDAO = newsInfoDAO;
	}

	public ErrorMsg addOrUpdateNewsInfo(NewsInfo newsInfo) throws Exception{
		ErrorMsg msg = new ErrorMsg();
		msg.setCode(Constant.SUCCESS);//success
		
		newsInfo.setPublishTime(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));
		
		if(newsInfo.getNewsId()>0){
			newsInfo.setPraiseCount(newsInfo.getPraiseCount() + newsInfo.getPraiseBaseCount());
			newsInfo.setCommentCount(newsInfo.getCommentCount() + newsInfo.getCommentBaseCount());
			
			newsInfoDAO.updateNewsInfo(newsInfo);
			
		}else{
			if(null != newsInfo && newsInfo.getPraiseBaseCount() > 0){
				newsInfo.setPraiseCount(newsInfo.getPraiseBaseCount());
			}
			if(null != newsInfo && newsInfo.getCommentBaseCount() > 0){
				newsInfo.setCommentCount(newsInfo.getCommentBaseCount());
			}
			
			newsInfoDAO.addNewsInfo(newsInfo);
		}
		
		return msg;
	}
	
	public PageModel getAddressInfos(PageModel model,String search,int sortId,int moduleType) throws Exception{
		return newsInfoDAO.getPagedInfos(model, search, sortId,moduleType);
	}
	
	public NewsInfo getInfoById(int infoId) throws Exception{
		NewsInfo newsInfo = newsInfoDAO.getById(infoId);
		return newsInfo;
	}
	
	public void delNewsInfo(int infoId) throws Exception{
		newsInfoDAO.delNewsInfo(infoId);
	}
	
	public List<NewsInfo> listAllInfos(int newsType) throws Exception{
		return newsInfoDAO.listAllInfos(newsType);
	}
	
}
